package com.csgc.camera.widget.mask

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import coil.load

import com.csgc.camera.constants.MaskConfig
import com.csgc.camera.storage.MaskPreference
import com.csgc.camera.util.dp
import com.cysd.spsyxj.R

import com.makeramen.roundedimageview.RoundedImageView

class MaskPinPai:MaskBase {

    private var name: TextView?=null
    private var slogan: TextView?=null
    private var logo: RoundedImageView?=null
    constructor(context: Context?,id:Int) : super(context,null){
        if (id == 0){
            LayoutInflater.from(context).inflate(R.layout.item_mask_pin_pai_1, this, true)
        }else if (id == 1){
            LayoutInflater.from(context).inflate(R.layout.item_mask_pin_pai_2, this, true)
        }
        dateStyle = "yyyy-MM-dd EEEE"

        if(MaskConfig.maskDateStyle.isNotEmpty()){
            dateStyle = MaskConfig.maskDateStyle
        }
        initView()

    }

    private fun initView(){
        location = findViewById(R.id.tv_pin_pai_location)
        remark = findViewById(R.id.tv_pin_pai_remark)
        date = findViewById(R.id.tv_pin_pai_date)
        customMaskContainer = findViewById(R.id.layout_custom_mask_container)

        name = findViewById(R.id.tv_pin_pai_name)
        slogan = findViewById(R.id.tv_pin_pai_slogan)
        logo = findViewById(R.id.iv_pin_pai_logo)

        location?.text = MaskConfig.maskLocation


        changeDateTime()

        val maskType = MaskPreference.instance.getMaskType()
//        MaskConfig.maskMap[0]?.forEach {
        /*MaskConfig.maskMap[maskType]?.forEach {
            if(it.title == "备注"){
                remark?.visibility = if(it.isOpen) View.VISIBLE else View.GONE
            }
        }*/

        MaskConfig.customMaskInfo.forEach {
            val text = TextView(context)
            text.setTextColor(Color.WHITE)
            text.setTextSize(TypedValue.COMPLEX_UNIT_SP,14f)
            text.text = "${it.title}:${it.content}"
            text.visibility = if (it.isOpen) View.VISIBLE else View.GONE

            customMaskContainer?.addView(text)
        }



        MaskConfig.maskMap[maskType]?.forEach {
            if(it.title == "备注"){
                remark?.text = "${it.title}:${it.content}"
                remark?.visibility = if(it.isOpen) View.VISIBLE else View.GONE
            }else if(it.title == "品牌logo"){
                val path = R.drawable.img_brand_default_logo
                if(it.content.isEmpty()){
                    logo?.load(path)
                }else{
                    logo?.load(it.content){
                        size(width=50.dp,height=50.dp)
                        allowHardware(false)
                    }
                }
            }
        }

    }

    override fun updateItemContent(title: String, content: String) {
        super.updateItemContent(title, content)
        if(title == "品牌名称"){
            name?.text = content
        }else if(title == "品牌宣传语/口号"){
            slogan?.text = content
        }else if(title == "品牌logo"){
            val path = R.drawable.img_brand_default_logo
            if(content.isEmpty()){
                logo?.load(path)
            }else{
                logo?.load(content){
                    size(width=50.dp,height=50.dp)
                    allowHardware(false)
                }
            }

        }

    }
}